<!-- 修改店铺 -->
<template>
  <div class="my-invoice-detail">  
    <row title="店铺名称" >
        <input class="ipt" type="text" slot="rowRight" placeholder="请输入店铺名称" v-model="shopName">
    </row>
    <group>
        <x-address  title="所在区域"
                    :raw-value="true" 
                    :value="value"
                    :list="addressData"  
                    @on-shadow-change="getValue"
                    placeholder="请选择地址">
        </x-address>
    </group>
    
    <row title="店铺地址">
        <input class="ipt" type="text" slot="rowRight" placeholder="请输入店铺地址" v-model="shopAdress">
    </row>
    <row title="联系人">
        <input class="ipt" type="text" slot="rowRight" placeholder="请输入联系人" v-model="cancat">
    </row>
    <row title="联系电话" >
        <input class="ipt" type="number" slot="rowRight" placeholder="请输入联系电话" v-model="cancatPhone">
    </row>
    <group v-if="shopType==3">
        <x-switch title="是否开启堂食" v-model="dine"></x-switch> 
        <x-switch title="是否开启外卖" v-model="takeOut"></x-switch> 
        <x-switch title="今日是否营业" v-model="open"></x-switch>
    </group> 
    <row title="最大配送距离(km)"  v-if="takeOut">
        <input class="ipt" type="number" slot="rowRight" placeholder="请输入最大配送距离" v-model="expressRange">
    </row>
    <row title="最低收取配送费公里数(km)" v-if="takeOut">
        <input class="ipt" type="number" slot="rowRight" placeholder="请输入公里数" style="width:2.2rem" v-model="chargeRange">
    </row>
    <row title="配送费(元)" v-if="takeOut">
        <input class="ipt" type="number" slot="rowRight" placeholder="请输入配送费" style="width:2.2rem" v-model="expressPrice">
    </row>
    <row title="营业时间(当天)"   v-if="shopType==3" > 
        <div slot="rowRight" class="time-select">
            <Datetime 
                class="start-time time-item"
                :min-hour="6"
                :max-hour = "23"
                v-model="todayBusinessStartTime"
                format="HH:mm"
            />
            <div class="lianjie">至</div>
            <Datetime 
                class="end-time time-item"
                :min-hour="6"
                :max-hour = "23" 
                v-model="todayBusinessEndTime"
                format="HH:mm"
            /> 
        </div>
    </row>
    <row title="营业时间(次日)"   v-if="shopType==3"> 
        <div slot="rowRight" class="time-select">
            <Datetime 
                class="start-time time-item" 
                :max-hour = "5"
                v-model="tomorrowBusinessStartTime"
                format="HH:mm"
            />
            <div class="lianjie">至</div>
            <Datetime 
                class="end-time time-item"
                :max-hour = "5" 
                v-model="tomorrowBusinessEndTime"
                format="HH:mm"
            /> 
        </div>
    </row>
    <row title="管理仓库" placeholder="请选择仓库" isclick :value="warehouseList[0] ? warehouseList[0].name : ''" @click.native="chooseWareHouse"></row>
    <div class="peisong-list" >
        <row :title="'配送时间段'+(index+1)"   v-for="(item,index) in psList" :key="index"> 
            <div slot="rowLeft" class="delete-time iconfont iconshanchu" @click="deleteTime(index)"> </div>
            <div slot="rowRight" class="time-select">
                <Datetime 
                    class="start-time time-item"
                    :min-hour = "6"
                    :max-hour = "23"
                    v-model="item.startTime"
                    format="HH:mm"
                />
                <div class="lianjie">至</div>
                <Datetime 
                    class="end-time time-item"
                    :min-hour = "6"
                    :max-hour = "23"
                    v-model="item.endTime"
                    format="HH:mm"
                /> 
            </div>
        </row>
    </div>
    <div class="add-time" v-if="takeOut" @click="addPsTime()">
        + <span>添加配送时段</span> 
    </div>
    <div class="invoice-photo">
        <div class="map-title">营业执照</div>
        <!-- 上传营业执照 文件 --> 
        <div class="wrap-box">
            <div class="wrpa-item" v-for="(item,index) in licensePics" :key="index">
                <div class="warp-img-box" >
                    <img    class="warp-img" :src="item"  alt="" />
                    <div class="iconfont iconguanbi " @click="delThis('1',index)"></div>
                </div>
            </div>
            <div class="uploader-box"  v-if="licensePics.length<9">
                <div class="add" @click="chooseType('1')">
                    <div class="add-image" align="center">
                        <i class="iconfont iconxiangji"></i>
                        <p class="font13">添加图片</p>
                    </div>
                </div>
            </div>  
        </div>
        
    </div>
    <div class="invoice-photo">
        <div class="map-title">设备二维码</div>
        <!-- 上传文件 -->
        <!-- 上传设备二维码 start --> 
        <div class="wrap-box">
            <div class="wrpa-item" v-for="(item,index) in deviceQrpics" :key="index">
                <div class="warp-img-box" >
                    <img    class="warp-img" :src="item"  alt="" />
                    <div class="iconfont iconguanbi " @click="delThis('2',index)"></div>
                </div>
            </div>
            <div class="uploader-box"  v-if="deviceQrpics.length<9">
                <div class="add" @click="chooseType('2')">
                    <div class="add-image" align="center">
                        <i class="iconfont iconxiangji"></i>
                        <p class="font13">添加图片</p>
                    </div>
                </div>
            </div>  
        </div>
        <!-- 上传设备二维码 end --> 
    </div>
    <div class="invoice-photo">
        <div class="map-title">店铺照片</div>

        <!-- 上传店铺照片 start --> 
        <div class="wrap-box">
            <div class="wrpa-item" v-for="(item,index) in storePics" :key="index">
                <div class="warp-img-box" >
                    <img    class="warp-img" :src="item"  alt="" />
                    <div class="iconfont iconguanbi " @click="delThis('3',index)"></div>
                </div>
            </div>
            <div class="uploader-box" v-if="storePics.length<9">
                <div class="add" @click="chooseType('3')">
                    <div class="add-image" align="center">
                        <i class="iconfont iconxiangji"></i>
                        <p class="font13">添加图片</p>
                    </div>
                </div>
            </div>  
        </div>
        <!-- 上传店铺照片 end --> 
    </div>
    <input @change="fileChange($event)" type="file" ref="inputer" id="upload_file" multiple style="display: none"/>
    <div class="height120"></div>
    <div class="btn isbtn" @click="submit()">提交</div>
  </div>
</template>

<script>
import row from '@/components/row'
import api from '@/api/api.js' 
import { XAddress,Group,ChinaAddressV4Data,XSwitch,Datetime  } from 'vux'
import { Message } from 'element-ui'
export default { 
  components: {
        row, 
        Group,
        XAddress,
        XSwitch,
        Datetime 
  },
  data () {
    return {
        value:[],
        addressData: ChinaAddressV4Data,
        shopName:'',
        shopAdress:'',
        psList:[],
        expressRange:'',
        chargeRange:'',
        expressPrice:'',
        cancat:'',
        cancatPhone:'',
        chooseIdx:'',
        storePics:[],
        licensePics:[],
        deviceQrpics:[],
        warehouseList: this.$store.state.warehouseList ,
        isBusiness:true,
        isHallFood:false,
        shopType:'',
        "open": true, 
        "dine": true,
        "takeOut":false,
        todayBusinessStartTime:'06:00',
        todayBusinessEndTime:'22:00',
        tomorrowBusinessStartTime:'未设置',
        tomorrowBusinessEndTime:'未设置',
    }
  },

    beforeRouteEnter (to, from, next) {
        console.log(from)
    if (from.name === "ShopDetail") {
        next( vm =>{
             vm.getDetail(vm.$route.query.id);
        });
       
    }else{
        next();
    }
    
    },
  activated(){
    this.warehouseList= this.$store.state.warehouseList ;
  },
  
  created(){
     
  },
  mounted () {
      
  },
  filters: {
     
  },
  chooseWareHouse(){
    this.$router.push({name:'chooseWarehouse'})
  },
  methods: {
    // 获取详情
    getDetail(id){
        api.shopMng.shopDetail({
            "storeId": id
        },res=>{
            console.log(res);
            if(res.result.licensePics.indexOf(",")!=-1){
                res.result.licensePics =  res.result.licensePics.split(",");
            }else{
                res.result.licensePics = res.result.licensePics? [res.result.licensePics] : [];
            }
            if(res.result.deviceQrpics.indexOf(",")!=-1){
                res.result.deviceQrpics =  res.result.deviceQrpics.split(",");
            }else{
                res.result.deviceQrpics = res.result.deviceQrpics ? [res.result.deviceQrpics] : [];
            }
            if(res.result.storePics.indexOf(",")!=-1){
                res.result.storePics =  res.result.storePics.split(",");
            }else{
                res.result.storePics = res.result.storePics ? [res.result.storePics] : [];
            }
            this.value = [res.result.province,res.result.city,res.result.district];
            this.shopType = res.result.type;
            this.shopName=res.result.storeName;
            this.shopAdress=res.result.address;
            this.cancat=res.result.contacts;
            this.cancatPhone=res.result.telephone;
            this.storePics=res.result.storePics;
            this.deviceQrpics=res.result.deviceQrpics;
            this.licensePics=res.result.licensePics; 
            this.warehouseList= res.result.replWarehouse ? [res.result.replWarehouse] : [];
            this.open = res.result.open;  //是否营业
            this.dine = res.result.dine;  // 是否堂食
            this.takeOut = res.result.takeOut ? true :false;  // 是否堂食
            this.todayBusinessStartTime = res.result.startTime || '未设置';
            this.todayBusinessEndTime =  res.result.endTime || '未设置';
            this.tomorrowBusinessStartTime =  res.result.nextStartTime || '未设置';
            this.tomorrowBusinessEndTime =  res.result.nextEndTime || '未设置'; 
            this.expressRange= res.result.expressRange;
            this.chargeRange= res.result.chargeRange;
            this.expressPrice= res.result.expressPrice;
            this.psList = res.result.takeoutTime || [];
        })

    },
    addPsTime(){ // 添加配送时段
        if(this.psList.length<3){
            this.psList.push({startTime:"未设置",endTime:"未设置"})
        }else{
            return  Message({
                message: "最多添加三个配送时段喔~",
                type: 'error',
                duration: 1 * 1000
            })
        }
        
    },
    getValue(ids,names) {
        this.value =names;
    },
    chooseWareHouse(){
      this.$router.push({path:'/chooseWarehouse'})
    },
    deleteTime(i){
        this.psList.splice(i,1);
    },
    submit(){
        if(!this.shopName){
            return  Message({
                message: "请输入店铺名！",
                type: 'error',
                duration: 1 * 1000
            })
        }
        if(!this.value[0]){
            return  Message({
                message: "请选择区域",
                type: 'error',
                duration: 1 * 1000
            })
        }
        if(!this.shopAdress){
            return  Message({
                message: "请填写地址",
                type: 'error',
                duration: 1 * 1000
            })
        }
        let takeoutConfig = {
                    "startTime":this.todayBusinessStartTime == '未设置' ? '' : this.todayBusinessStartTime,
                    "endTime":this.todayBusinessEndTime== '未设置' ? '' : this.todayBusinessEndTime,
                    "nextStartTime":  this.tomorrowBusinessStartTime == '未设置' ? '' : this.tomorrowBusinessStartTime,
                    "nextEndTime": this.tomorrowBusinessEndTime == '未设置' ? '' : this.tomorrowBusinessEndTime,
                }
        if(this.takeOut){
            if(!this.expressRange || this.expressRange % 1 !=0){
                return  Message({
                    message: "请填写整数的配送距离",
                    type: 'error',
                    duration: 1 * 1000
                })
            }
            if(this.chargeRange &&  this.chargeRange % 1 !=0){
                return  Message({
                    message: "最低收取配送费公里数必须为整数",
                    type: 'error',
                    duration: 1 * 1000
                })
            }
            if(this.expressPrice &&  this.expressPrice % 1 !=0){
                return  Message({
                    message: "配送费必须为整数",
                    type: 'error',
                    duration: 1 * 1000
                })
            }
            takeoutConfig.expressRange = parseInt(this.expressRange);
            takeoutConfig.chargeRange = this.chargeRange ? parseInt(this.chargeRange) : ''; //超出？公里收取
            takeoutConfig.expressPrice = this.expressPrice ?  parseInt(this.expressPrice)  : '';//配送费  
            if(this.psList.length>0){
                let psArr=[]; 
                if(this.psList.length>1 && !this.checkTimes()){
                    return  Message({
                        message: "时间配置存在交集",
                        type: 'error',
                        duration: 1 * 1000
                    })
                }
                for(var i=0;i<this.psList.length;i++){

                    if(this.psList[i].startTime!="未设置" && this.psList[i].endTime!="未设置"){
                        let startTime = new Date("2022-11-24 " + this.psList[i].startTime).getTime();
                        let endTime = new Date("2022-11-24 " + this.psList[i].endTime).getTime();
                        if(startTime  > endTime){  
                            return  Message({
                                message: "时间配置错误",
                                type: 'error',
                                duration: 1 * 1000
                            })
                        }
                        psArr.push({startTime:this.psList[i].startTime,endTime:this.psList[i].endTime})
                    }else{
                        return  Message({
                            message: "请填写配送时段" + (i+1),
                            type: 'error',
                            duration: 1 * 1000
                        })
                    }
                    
                }
                takeoutConfig.takeoutTime = psArr;
            }
        }

        
        api.shopMng.updateShop({
            storeId:this.$route.query.id,
            "storeName":this.shopName,
            "contacts":this.cancat,
            "telephone":this.cancatPhone,
            "province":this.value[0],
            "city":this.value[1],
            "district":this.value[2],
            "storePics":this.storePics.join(","),
            "licensePics":this.licensePics.join(","),
            "deviceQrpics":this.deviceQrpics.join(","),
            "address":this.shopAdress,
            replWarehouseId:this.warehouseList[0] ? this.warehouseList[0].id : '',
            "storeConfig":{
                "storeId":this.$route.query.id,
                "dine":this.dine,
                "takeout":this.takeOut,
                "open":this.open,
                "takeoutConfig":JSON.stringify(takeoutConfig)
            } 

        },res=>{
            this.$router.go(-1);
            return  Message({
                message: "更新成功",
                type: 'success',
                duration: 1 * 1000
            })

        })
    },
    checkTimes() {
        //简单点，双重循环，判断每一个时间和其他任意一个时间有没有交集
        const arr= this.psList;
        for (let i = 0; i < arr.length; i++) {
            let start1 = new Date("2022-11-24 " + arr[i].startTime);
            let end1 = new Date("2022-11-24 " + arr[i].endTime);
            for (let j = i+1; j < arr.length; j++) {
                let start2 = new Date("2022-11-24 " + arr[j].startTime);
                let end2 = new Date("2022-11-24 " + arr[j].endTime);
                //交集判断情况较多，我们采用非交集取反的方式，
                //非交集有两种情况，12AB  AB12
                //即1结束时间小于等于2开始时间或者1开始时间大于等于2结束时间
                if (!(end1 <= start2 || start1 >= end2)) {
                    return false;  //存在交集
                }
            }
        }
        return true;  //不存在交集
    },
    //delThis 删除某一项的照片
    delThis(idx,index){
        if(idx==1){ //营业执照
            this.licensePics.splice(index,1)
        }else if(idx==2){
            this.deviceQrpics.splice(index,1)
        }else if(idx==3){
            this.storePics.splice(index,1)
        }
    }, 
    //上传照片
    chooseType(idx) {
        this.chooseIdx=idx;
        document.getElementById('upload_file').click();
    },
    fileChange(el) {
        let file = this.$refs.inputer.files;
        console.log(file);
        api.publick.uploadFilePost({files:file},
            res=>{
                if(res.data.resultCode==0){
                    console.log("上传成功",res);
                    if(this.chooseIdx==1){//营业执照
                        this.licensePics.push(res.data.result);
                    }else if(this.chooseIdx==2){//设备二维码
                        this.deviceQrpics.push(res.data.result);
                    }else if(this.chooseIdx==3){//店铺照片
                        this.storePics.push(res.data.result);
                    }
                }
            }
        )
        
    },
  }
}
</script>
<style lang="less" scoped>
/deep/.time-select{
    display: flex;
    align-items: center;
    .time-item{
        background: #eee;
        border-radius: .08rem;
        padding: .08rem .20rem !important;
        color: #888;
    }
    .lianjie{
        margin: 0 .12rem;
    }
}
// /deep/.vux-x-switch{
//     transform: scale(0.7);
//     padding: 0 !important;
// }
/deep/.vux-no-group-title{
    margin-top: 0;
}
/deep/.weui-cells:before{
    display: none;
}
/deep/.weui-cells::after{
    left: 20px;
}
.wrap-box{
    display: flex;
    flex-wrap: wrap;
    .warp-img{
        width: 80px;
        height: 80px;
    }
    .iconguanbi{
        color: #fbfbfb;
        background: rgba(33,33,33,.7);
        font-size: .36rem;
        top: 2px;
        right: 12px;
        position: absolute;
    }
}
.iconshanchu{
    color: red;
}
.warp-img-box{
    position: relative;
}
.uploader-box{
    width: 100px;
    height: 100px;
}
.invoice-photo{
    padding: 12px 16px;
}
.map-title{
    height: 40px;
    line-height: 40px;
    font-size: 16px;
    font-weight: bold;
}
.invoice-photo-img{
   width: 100%;
   min-width: 74px;
   min-height: 74px; 
}
input {
    outline: none;
}
.ipt{
    border: none;
}
.invoice-order{
    display: flex;
    height: 40px;
    line-height: 40px;

}
.invoice-number{
    color: #444;
    width: 35%;
    padding-left: 8px;
}
.tips{
    color: #999;
    font-size: 11px;
    margin-left: 8px;
}
.invoice-money{
    color: #999;
    width: 30%;
}
.invoice-timer{
   color: #666; 
   width: 35%;
}
.invoice-active{
    background: #e1e1e1;
}

.wrap-box{
    display: flex;
    flex-wrap: wrap;
    img{
        margin-bottom: 10px;
        margin-right: 10px;
        width: 74px;
        height: 74px;    
    }
}
.height120{
    height: 60px;
}
.btn{
    text-align: center;
    line-height: 50px;
    background: #FABE00;
    color: #fff;
    height: 50px;
    font-size: 16px;
    letter-spacing: 4px;
    position: fixed;
    width: 100%;
    bottom: 0;
    left: 0;
}
.add-img {
    width: 100%;
    padding: 10px;
}

.add-img p {
    color: #999;
}
.add-image {
    padding-top: 15px; 
    width: 80px; 
    height: 80px;
    border: 1px dashed rgba(0, 0, 0, .2);
}

.add-image .iconxiangji {
    font-size: 24px;
}
.add-time{
    width: 6.9rem;
    margin: .24rem auto;
    height: .78rem;
    line-height: .78rem;
    color: #FABE00;
    border: 1px solid #FABE00;
    text-align: center;
    border-radius: .12rem;
    span{
        margin-left: .12rem;
    }
}
</style>
